package com.xyre.client.business.index.view;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;

import com.xyre.client.R;
import com.xyre.client.business.base.BaseActivity;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    private Fragment indexFragment;

    private Fragment shopFragment;

    private Fragment serviceFragment;

    private Fragment myFragment;

    private FragmentManager fragmentManager;

    private FragmentTransaction transaction;

    private RadioGroup mainRadioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        indexFragment = new IndexFragment();
        shopFragment = new ShopFragment();
        serviceFragment = new ServiceFragment();
        myFragment = new MyFragment();
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.content,indexFragment)
                .add(R.id.content,shopFragment)
                .add(R.id.content,serviceFragment)
                .add(R.id.content,myFragment)
                .commit();
        initDisplay();
    }

    private void initDisplay() {
        mainRadioGroup = (RadioGroup) findViewById(R.id.rg_main);
        mainRadioGroup.setOnCheckedChangeListener(this);
        mainRadioGroup.check(R.id.rb_main_index);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (checkedId) {
            case R.id.rb_main_index:
                if (indexFragment == null) {
                    // 如果IndexFragment为空，则创建一个并添加到界面上
                    indexFragment = new IndexFragment();
                    transaction.add(R.id.content, indexFragment);
                } else {
                    // 如果MarketFragment不为空，则直接将它显示出来
                    transaction.show(indexFragment);
                }
                break;
            case R.id.rb_main_shop:
                if (shopFragment == null) {
                    // 如果MarketFragment为空，则创建一个并添加到界面上
                    shopFragment = new ShopFragment();
                    transaction.add(R.id.content, shopFragment);
                } else {
                    // 如果MarketFragment不为空，则直接将它显示出来
                    transaction.show(shopFragment);
                }
                break;
            case R.id.rb_main_service:
                if (serviceFragment == null) {
                    // 如果MarketFragment为空，则创建一个并添加到界面上
                    serviceFragment = new ShopFragment();
                    transaction.add(R.id.content, serviceFragment);
                } else {
                    // 如果MarketFragment不为空，则直接将它显示出来
                    transaction.show(serviceFragment);
                }
                break;
            case R.id.rb_main_my:
                if (myFragment == null) {
                    // 如果MarketFragment为空，则创建一个并添加到界面上
                    myFragment = new MyFragment();
                    transaction.add(R.id.content, myFragment);
                } else {
                    // 如果MarketFragment不为空，则直接将它显示出来
                    transaction.show(myFragment);
                }
                break;
            default:
                break;
        }
        transaction.commit();
    }


    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction 用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (indexFragment != null) {
            transaction.hide(indexFragment);
        }
        if (shopFragment != null) {
            transaction.hide(shopFragment);
        }
        if (serviceFragment != null) {
            transaction.hide(serviceFragment);
        }
        if (myFragment != null) {
            transaction.hide(myFragment);
        }
    }
}
